/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.utils.types;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Immutable convenience wrapper for a GregorianCalendar.
 */
public class Cal {

    private final GregorianCalendar calendar;

    public Cal() {
        calendar = new GregorianCalendar();
    }

    public Cal(DateTime date) {
        this();
        calendar.setTime(date.getDate());
    }

    public Cal(GregorianCalendar calendar) {
        this.calendar = calendar;
    }

    public GregorianCalendar getCalendar() {
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(new Date(calendar.getTime().getTime()));
        return c;
    }

    public Cal add(int field, int amount) {
        GregorianCalendar newCal = (GregorianCalendar) calendar.clone();
        newCal.add(field, amount);
        return new Cal(newCal);
    }

    public Cal set(int field, int value) {
        GregorianCalendar newCal = (GregorianCalendar) calendar.clone();
        newCal.set(field, value);
        return new Cal(newCal);
    }

    public Cal set(int year, int month, int day) {
        GregorianCalendar newCal = (GregorianCalendar) calendar.clone();
        newCal.set(Calendar.YEAR, year);
        newCal.set(Calendar.MONTH, month);
        newCal.set(Calendar.DAY_OF_MONTH, day);
        return new Cal(newCal);
    }

    public Cal set(int hour, int minutes, int seconds, int milliseconds) {
        GregorianCalendar newCal = (GregorianCalendar) calendar.clone();
        newCal.set(Calendar.HOUR_OF_DAY, hour);
        newCal.set(Calendar.MINUTE, minutes);
        newCal.set(Calendar.SECOND, seconds);
        newCal.set(Calendar.MILLISECOND, milliseconds);
        return new Cal(newCal);
    }

    public int get(int field) {
        return calendar.get(field);
    }

    public DateTime getDateTime() {
        return new DateTime(calendar.getTime());
    }

    public boolean before(DateTime dateTime) {
        return calendar.getTime().before(dateTime.getDate());
    }

    public boolean before(Cal cal) {
        return calendar.getTime().before(cal.calendar.getTime());
    }

    public boolean after(DateTime dateTime) {
        return calendar.getTime().after(dateTime.getDate());
    }

    public boolean after(Cal cal) {
        return calendar.getTime().after(cal.calendar.getTime());
    }

    public long getTicks() {
        return getDateTime().getTicks();
    }

    public Cal beginningOfDay() {
        return set(0, 0, 0, 0);
    }

    public Cal endOfDay() {
        return beginningOfDay().add(Calendar.DATE, 1);
    }

    /**
     * @return the number of milliseconds since the start of the day.
     */
    public int getTimeOfDayValue() {
        return
                calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 +
                        calendar.get(Calendar.MINUTE) * 60 * 1000 +
                        calendar.get(Calendar.SECOND) * 1000 +
                        calendar.get(Calendar.MILLISECOND);
    }

    /**
     * @return a value making the date excluding the time of day comparable.
     */
    public int getDateValue() {
        return
                calendar.get(Calendar.YEAR) * 1000 +
                        calendar.get(Calendar.DAY_OF_YEAR);
    }

    public String format(DateFormat dateFormat) {
        return dateFormat.format(calendar.getTime());
    }

}
